package com.uphone.kingmall.fragment.business_area;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.china.zhuoyue.R;
import com.google.zxing.WriterException;
import com.uphone.kingmall.activity.ShopPhotoActivity;
import com.uphone.kingmall.base.BaseFragment;
import com.uphone.kingmall.bean.ShopInfoBean;
import com.uphone.kingmall.utils.ConstansUtils;
import com.uphone.kingmall.utils.DensityUtil;
import com.uphone.kingmall.utils.DialogUtil;
import com.uphone.kingmall.utils.MapSoftUtil;
import com.uphone.kingmall.utils.QRCodeUtils;
import com.uphone.kingmall.utils.ToastUtils;
import com.uphone.kingmall.utils.intent.IntentUtils;
import com.uphone.kingmall.view.dialog.RadishDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <pre>
 *     author : 孙增艳
 *     e-mail : sunzengyan@outlook.com
 *     time   : 2019/2/28
 *     desc   : 店铺详情
 *     version: 1.0
 * </pre>
 */
public class ShopDetailsFragment extends BaseFragment {
    private static ShopDetailsFragment fragment;
    @BindView(R.id.tv_zhangguiming)
    TextView tvZhangguiming;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    private ShopInfoBean.ShopBean shop;
    private String shopAddr;
    private String lat;
    private String lng;

    public static ShopDetailsFragment newInstance() {
        if (fragment == null) {
            fragment = new ShopDetailsFragment();
        }
        return fragment;
    }

    public static ShopDetailsFragment getFragment() {
        return fragment;
    }

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_fragment_shop_details;
    }

    @Override
    public void initData() {
        if (shop != null) {
            loadData(shop);
        }
    }

    @Override
    public void initView(View view) {

    }

    @OnClick({R.id.rl_dpmp, R.id.rl_ckxc, R.id.rl_phone, R.id.tv_address})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_dpmp://店铺名片
                if (shop != null && shop.getShopId() == null) {
                    ToastUtils.showShortToast(getActivity(), "无法获取店铺信息");
                    return;
                }
                View dialogView = View.inflate(getActivity(), R.layout.kingmall_dialog_qrcode, null);
                ImageView iv_qrcode = dialogView.findViewById(R.id.iv_qrcode);
                //绘制二维码
                try {
                    Bitmap bitmap = QRCodeUtils.createQRCode
                            (ConstansUtils.QRCodeShop + shop.getShopId() + "", DensityUtil.dip2px(getActivity(), 260));
                    iv_qrcode.setImageBitmap(bitmap);
                } catch (WriterException e) {
                    e.printStackTrace();
                }
                new RadishDialog.Builder(getActivity()).setView(dialogView)
                        .show();
                break;
            case R.id.rl_ckxc://查看相册
                if (shop.getShopId() == null) {
                    ToastUtils.showShortToast(getActivity(), "无法获取店铺信息");
                    return;
                }
                IntentUtils.getInstance().with(getActivity(), ShopPhotoActivity.class).putInt(ConstansUtils.ID, shop.getShopId()).start();
                break;
            case R.id.rl_phone://拨打电话

                if (shop != null && shop.getShopPhone() != null) {
                    DialogUtil.showCallDialog(getActivity(), shop.getShopPhone());
                }
                break;
            case R.id.tv_address://地图  114.476767,38.011628
                MapSoftUtil.goToNaviActivity(getActivitySafely(), shopAddr, lat, lng);

                break;
            default:
                break;
        }
    }

    public void loadData(ShopInfoBean.ShopBean shop) {
        this.shop = shop;
        if (shop != null) {
            if (tvZhangguiming != null) {
                tvZhangguiming.setText(shop.getShopOwnerName() + "");
            }
            if (tvAddress != null) {
//                tvAddress.setText(shop.getShopRegion() + shop.getShopAddr() + "");
//                shopAddr = shop.getShopAddr();
                tvAddress.setText(shop.getBusinessAddress() == null ? "" : shop.getBusinessAddress());
                shopAddr = shop.getBusinessAddress() == null ? "" : shop.getBusinessAddress();
            }
            if (tvMobile != null) {
                tvMobile.setText(shop.getShopPhone() + "");
            }
            if (tvTime != null) {
                tvTime.setText(shop.getOpenDate() + "");
            }
            lat = shop.getLat();
            lng = shop.getLng();
        }
    }
}
